using System;

namespace SimpleObjectPool
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PoolClass<T> : Pool<T> where T : class
    {
        private readonly Action<T> mResetMethod;

        public PoolClass(Func<T> factoryMethod, Action<T> resetMethod = null, int initCount = 0)
        {
            mFactory = new FactoryClass<T>(factoryMethod);
            mResetMethod = resetMethod;

            for (int i = 0; i < initCount; i++)
            {
                mCacheStack.Push(mFactory.Create());
            }
        }

        public override bool Return(T obj)
        {
            mResetMethod?.Invoke(obj);
            mCacheStack.Push(obj);
            return true;
        }
    }

    public class FactoryClass<T> : IObjectFactory<T> where T: class
    {
        protected Func<T> funcFactory;

        public FactoryClass(Func<T> pFuncFactory)
        {
            funcFactory = pFuncFactory;
        }

        public T Create()
        {
            return funcFactory();
        }
    }
}